package amex;

import java.io.File;
import java.io.FilenameFilter;

import javax.swing.filechooser.FileFilter;

public class AmericanExpressFileFilter extends FileFilter {
	String description;
	String expressions[];

	public AmericanExpressFileFilter(String description, String extension) {
		this(description, new String[] { extension });
	}

	public AmericanExpressFileFilter(String description, String extensions[]) {
		if (description == null) {
			this.description = extensions[0];
		} else {
			this.description = description;
		}
		this.expressions = (String[]) extensions.clone();
		toLower(this.expressions);
		
	}

	private void toLower(String array[]) {
		for (int i = 0, n = array.length; i < n; i++) {
			array[i] = array[i].toLowerCase();
		}
	}

	@Override
	public boolean accept(File file) {
		if (file.isDirectory()) {
			return true;
		} else {
			String path = file.getAbsolutePath().toLowerCase();
			for (int i = 0, n = expressions.length; i < n; i++) {
				String expression = expressions[i];
				if (path.startsWith(expression)) {
					return true;
				}
			}
		}
		return false;
	}

	@Override
	public String getDescription() {
		return description;
	}

}
